package com.cljz.makepassenger.present.activity;

import android.os.Build;
import android.telephony.TelephonyManager;

import com.cljz.makepassenger.base.App;
import com.cljz.makepassenger.base.BaseP;
import com.cljz.makepassenger.model.net.api.Api;
import com.cljz.makepassenger.model.net.bean.ResponseBean;
import com.cljz.makepassenger.ui.activity.FeedbackActivity;
import com.google.gson.Gson;

import java.util.HashMap;

import cn.droidlover.xdroidmvp.net.ApiSubcriber;
import cn.droidlover.xdroidmvp.net.NetError;
import cn.droidlover.xdroidmvp.net.XApi;
import cn.droidlover.xdroidmvp.net.diy.Base64Utils;
import okhttp3.RequestBody;

import static android.content.Context.TELEPHONY_SERVICE;

/**
 * 进行绑定，减少冗余，网络相关配置
 * Created by Administrator on 2017/4/5 0005.
 */
public class FeedbackP extends BaseP<FeedbackActivity> {

    public void Feedback(String content) {
        Gson gson = new Gson();
        HashMap<String, String> paramsMap = new HashMap<>();
        String mobileVersion = Build.MODEL;//手机型号,开启服务
        TelephonyManager tm = (TelephonyManager) App.getInstance().getSystemService(TELEPHONY_SERVICE);
        String IMEI = tm.getDeviceId();//IMEI
        String versionRelease = Build.VERSION.RELEASE;//系统版本
        //直接粘贴到这里就可以了
        paramsMap.put("token", getToken());
        //paramsMap.put("token","GBE1Njg0ODMxNzA1MzE0NDMyMRgYRm95bHFLWG9WaGFBdE1DQXlNXzd5UUVBAA");
        paramsMap.put("phone_info", mobileVersion + "," + IMEI + "," + versionRelease);
        paramsMap.put("content", content);
        String body = Base64Utils.enCode(gson.toJson(paramsMap));
        RequestBody requestBody = RequestBody.create(okhttp3.MediaType.parse("text/plain;charset=UTF-8"), body);
        Api.getResponseInfoAPI().getFeedbackData(requestBody)
                .compose(XApi.<ResponseBean>getApiTransformer())         //异常处理
                .compose(XApi.<ResponseBean>getScheduler())              //线程切换
                .compose(getV().<ResponseBean>bindToLifecycle())
                .subscribe(new ApiSubcriber<ResponseBean>() {
                    //提交成功要做的事情
                    @Override
                    public void onNext(ResponseBean feedbackBean) {
                        if ("0".equals(feedbackBean.getError_code())) {
                            getV().FeedbackSucceed();
                        } else if ("1000".equals(feedbackBean.getError_code())) {
                            getV().loginOvertime(1);
                        } else {
                            getV().FeedbackFail(feedbackBean.getMsg());
                        }
                    }

                    @Override
                    protected void onFail(NetError error) {
                    }

                });
    }
}
